/**
 * ajax
 *
 */
export const ajax = (url: string, method: string, params: object = {}): any => {
    const result = new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest()
        xhr.open(method, url, true)
        xhr.send(JSON.stringify(params))
        xhr.onreadystatechange = () => {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    const res = JSON.parse(xhr.responseText)
                    resolve(res)
                } else {
                    reject(xhr.statusText)
                }
            }
        }
        if (method === 'POST' || method === 'PUT') {
            xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8')
        }
    })

    return result
}
